package bb.lanxing.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import bb.lanxing.R;

public class LoadingView extends FrameLayout {
    private TextView mLoadingText;
    private ProgressBar mProgressBar;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.layout_load_more, this);
        this.mProgressBar = (ProgressBar) findViewById(R.id.loading_progress);
        this.mLoadingText = (TextView) findViewById(R.id.loading_text);
        setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
        complete();
    }

    public void failure() {
        this.mLoadingText.setVisibility(View.VISIBLE);
        this.mProgressBar.setVisibility(View.GONE);
        this.mLoadingText.setText(R.string.list_load_failed);
    }

    public void complete() {
        this.mLoadingText.setVisibility(View.VISIBLE);
        this.mProgressBar.setVisibility(View.GONE);
        this.mLoadingText.setText(R.string.list_load_more);
    }

    public void loading() {
        this.mLoadingText.setVisibility(View.VISIBLE);
        this.mProgressBar.setVisibility(View.VISIBLE);
        this.mLoadingText.setText(R.string.loading);
    }

    public static LoadingView newInstanceFromLayout(ViewGroup parent) {
        return (LoadingView) LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_loading_view, parent, false);
    }
}
